using System;
using System.Text.RegularExpressions;
using System.Collections;

namespace Antix.Mail
{
	/// <summary>
	/// Contact object
	/// </summary>
	public class Contact
	{
		#region Constructors etc.

		/// <summary>
		/// Create a contact
		/// </summary>
		/// <param name="details">Contact details</param>
		public Contact( string details )
		{
			Match match = Regex.Match( details, @"""{0,1}(.*?)""{0,1}\s*[<\[](.*?)[>\[]" );
			if( match != null )
			{
				if( match.Groups.Count == 3 )
				{
					// e-mail and name
					_Name = match.Groups[ 1 ].Value;
					_EMail = match.Groups[ 2 ].Value;
				}
				else
				{
					// assume e-mail only
					_Name = "";
					_EMail = match.Groups[ 1 ].Value;
				}
			}
			else
			{
				// unknown details format
				_Name = "";
				_EMail = "";
			}
		}

		#endregion

		#region Properties

		#region Name

		private string _Name;

		/// <summary>
		/// Name of the MailBox
		/// </summary>
		public string Name
		{
			get{ return _Name; }
			set{ _Name = value; }
		}

		#endregion

		#region EMail

		private string _EMail;

		/// <summary>
		/// EMail of the MailBox
		/// </summary>
		public string EMail
		{
			get{ return _EMail; }
			set{ _EMail = value; }
		}

		#endregion

		#endregion

		#region Method

		#region ToEMailString

		/// <summary>
		/// Output standard contact format for email
		/// </summary>
		/// <returns>Details</returns>
		public string ToEMailString()
		{
			if( _Name.Length > 0 )
			{
				return "\"" + _Name + "\" <" + _EMail + ">";
			}
			else
			{
				return _EMail;
			}
		}

		#endregion

		#region ToString

		/// <summary>
		/// Display format
		/// </summary>
		/// <returns>Details</returns>
		public override string ToString()
		{
			if( _Name.Length > 0 )
			{
				return _Name + " (" + _EMail + ")";
			}
			else
			{
				return _EMail;
			}
		}

		#endregion

		#endregion
	}
}
